package cn.example.demo.modules.house.service.impl;

import cn.example.demo.common.model.service.ServiceResult;
import cn.example.demo.common.retrieval.PageBean;
import cn.example.demo.common.secure.annotation.DataScopeEnable;
import cn.example.demo.common.secure.context.LoginUserApi;
import cn.example.demo.common.secure.dto.LoginUser;
import cn.example.demo.common.tools.QueryServiceUtils;
import cn.example.demo.common.tools.obj.DateAgeUtils;
import cn.example.demo.common.tools.obj.reflect.EntityUtils;
import cn.example.demo.modules.house.dto.HouseImageDto;
import cn.example.demo.modules.house.entity.HouseImage;
import cn.example.demo.modules.house.mapper.HouseImageMapper;
import cn.example.demo.modules.house.service.IHouseImageService;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述：【房源图片】接口实现类
 */

@Service
public class HouseImageServiceImpl extends ServiceImpl<HouseImageMapper, HouseImage> implements IHouseImageService {
    @Autowired
    private LoginUserApi loginUserApi;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult insertHouseImage(HouseImageDto houseImageDto) {
        HouseImage entity = EntityUtils.entityConvert(houseImageDto, new HouseImage(), false);

//        entity.setImageStatus(1);

        if (this.save(entity)) {
            return ServiceResult.isSuccess(StrUtil.format("【房源图片，ID:{}】创建成功!", entity.getId()), entity);
        }

        return ServiceResult.isInternalError("【房源图片】创建失败，未知错误！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult updateHouseImage(HouseImageDto houseImageDto) {
        HouseImage entity = this.getById(houseImageDto.getId());
        if (entity == null) {
            return ServiceResult.isNotFound(StrUtil.format("【房源图片】信息更新失败：记录不存在！ID:{}", houseImageDto.getId()));
        }

        entity = EntityUtils.entityConvert(houseImageDto, entity, true);
        if (this.updateById(entity)) {
            return ServiceResult.isSuccess(StrUtil.format("【房源图片，ID:{}】信息更新成功!", entity.getId()), null);
        }

        return ServiceResult.isNotModified("未知错误，【房源图片】信息修改失败！");
    }

    @Override
    public PageBean queryHouseImage(HouseImageDto houseImageDto) {
        Page pageResult = PageHelper.startPage(houseImageDto.getPageNum(), houseImageDto.getPageSize())
                .doSelectPage(() -> this.list(getQueryWrapper(houseImageDto)));

        return QueryServiceUtils.encapsulatePageBean(new PageBean<>(), pageResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult deleteHouseImage(HouseImageDto houseImageDto) {
        int rows = this.baseMapper.deleteBatchIds(houseImageDto.getIds());
        if (rows > 0) {
            return ServiceResult.isSuccess(StrUtil.format("操作成功，已批量删除【房源图片】共 {} 条记录！", rows), rows);
        }

        return ServiceResult.isNotModified("删除【房源图片】失败，请稍后重试！");
    }

    @Override
    public HouseImage findHouseImageById(String id) {
        return this.getById(id);
    }

    @Override
    public List<Map<String, Object>> queryHouseImageAsDict(HouseImageDto houseImageDto) {
        return this.baseMapper.selectAsDictList(getQueryWrapper(houseImageDto));
    }

    /**
     * 查询条件
     *
     * @param houseImageDto
     * @return
     */

    protected LambdaQueryWrapper getQueryWrapper(HouseImageDto houseImageDto) {
        LambdaQueryWrapper<HouseImage> wrapper = Wrappers.lambdaQuery();

        wrapper
            .eq(StrUtil.isNotBlank(houseImageDto.getId()), HouseImage::getId, houseImageDto.getId())

            .like(StrUtil.isNotBlank(houseImageDto.getHouseId()), HouseImage::getHouseId, houseImageDto.getHouseId())

            .like(StrUtil.isNotBlank(houseImageDto.getImageUrl()), HouseImage::getImageUrl, houseImageDto.getImageUrl())

            .eq(houseImageDto.getSort() != null, HouseImage::getSort, houseImageDto.getSort())
    
            .eq(houseImageDto.getImageStatus() != null, HouseImage::getImageStatus, houseImageDto.getImageStatus())
    ;

        wrapper.orderByAsc(HouseImage::getSort);
        return wrapper;
    }
}
